package com.base.cn.platform.os.controller.web.op.affiche;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.service.manage.op.affiche.OpReceiveAfficheService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 前台消息Controller
 *
 * @author zsw
 * @since 20118/6/1
 */
@Controller
@RequestMapping("/web/uc/affiche")
public class WebAfficheController extends WebBaseController {

    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private OpReceiveAfficheService receiveAfficheService;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 获取用户私信列表
     *
     * @return
     */
    @RequestMapping("/queryUserLetterList")
    public String queryUserLetterList(HttpServletRequest request) {
        return resultPage("/op/receiveAffiche/user-letter");
    }

    /**
     * 获取用户私信详情
     *
     * @return
     */
    @RequestMapping("/queryUserLetterInfo")
    public String queryUserLetterInfo(HttpServletRequest request) {
        return resultPage("/op/receiveAffiche/user-letter-info");
    }

    /**
     * 获取用户消息
     *
     * @return
     */
    @RequestMapping("/queryUserAfficheList")
    public String queryUserAfficheList(HttpServletRequest request) {
        //TODO 重置未读消息数为0 redisCache.set(USER_MSG_NUM_TAG + userId, "0", CacheConstants.ONE_DAY); 添加消息到用户接收表
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        receiveAfficheService.saveReceiveAffichBatch(userId);

        return resultPage("/op/receiveAffiche/user-news");
    }

    /**
     * 获取用户消息列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/queryUserAfficheList")
    public String ajaxQueryUserAfficheList(Model model,
                                           HttpServletRequest request,
                                           @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> cusUser = cusUserService.userInfo(loginUserId, false, false, false, false, false, false, false);
        cusUser.replace("lastSystemTime", new Date());
        cusUserService.updateUser(cusUser);
        HashMap<String, Object> receiveAffiche = new HashMap<>();
        receiveAffiche.put("userId", loginUserId);
        receiveAffiche.put("order", 2);
        Map<String, Object> afficheMap = receiveAfficheService.queryAjaxReceiveAfficheList(currentPage, receiveAffiche, true, true, true);
        model.addAttribute("dataMap", afficheMap);
        return resultPage("/op/receiveAffiche/user-ajax-news");
    }

    /**
     * 用户删除消息
     *
     * @param id 接收消息ID
     * @return
     */
    @RequestMapping("/deleteReceiveAffiche")
    @ResponseBody
    public Map<String, Object> deleteReceiveAffiche(HttpServletRequest request,
                                                    @RequestParam("id") BigDecimal id) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        customCacheChannel.evict(J2CacheRegion.ONE_DAY_REGION.getKey(),CacheConstants.USER_MSG_NUM_TAG + userId);
        return receiveAfficheService.deleteReceiveAffiche(id);
    }

    /**
     * 修改用户消息状态(已读)
     *
     * @return request
     */
    @RequestMapping("/updateReceiveAfficheStatus")
    @ResponseBody
    public Map<String, Object> updateReceiveAfficheStatus(HttpServletRequest request) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return receiveAfficheService.updateReceiveArticleStatusById(userId);
    }

    /**
     * 获取消息数量
     *
     * @param request
     * @return
     */
    @RequestMapping("/countReceiveAffiche")
    @ResponseBody
    public Map<String,Object> countReceiveAffiche(HttpServletRequest request) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return receiveAfficheService.countReceiveAffiche(userId);
    }

}
